// eslint-disable-next-line import/named
import { ActionTree } from 'vuex'
import { StateInterface } from '../state'
import { ContentStateInterface } from './state'
import iContent from 'src/api/content'
import iUser from 'src/api/user'

const actions: ActionTree<ContentStateInterface, StateInterface> = {
  async initMenu ({ commit }, data) {
    // 获取菜单
    // if (state.menu.isLoaded) return
    const menuList = await iContent.getMenu(data)
    commit('setMenuList', menuList)
  },
  async initContent ({ commit }, {
    ids,
    keywords,
    sorts
  }: { ids: string[], keywords?: string, sorts: string }) {
    const contents = await iContent.findContents({
      menus: (ids || []).filter(e => e),
      keywords,
      sorts
    })
    commit('setContentList', contents)
  },
  async initUserContent ({ commit }, ids: string[]) {
    ids = ids || []
    if (ids) {
      const contents = await iContent.getContentByIds({
        ids: ids
      })
      commit('setContentList', contents)
    }
  },
  async initCurrentContent ({ commit }, id) {
    const contents = await iContent.getContentByIds({
      ids: [id]
    })
    commit('setCurrentContent', contents[0])
  },
  async addContentVote ({ commit }, content) {
    const id = content._id
    await iUser.postActivity({
      level: 1,
      type: 2,
      location: {
        module: 'km',
        page: 'view',
        element: 'vote-icon',
        event: 'click'
      },
      action: 'content-vote',
      value: { id: id }
    })
    commit('addContentVote', content)
  },
  async popContentVote ({ commit }, content) {
    const id = content._id
    await iUser.postActivity({
      level: 1,
      type: 2,
      location: {
        module: 'km',
        page: 'view',
        element: 'vote-icon',
        event: 'click'
      },
      action: 'cancel-content-vote',
      value: { id: id }
    })
    commit('popContentVote', content)
  },
  async addContentCollection ({ commit }, content) {
    const id = content._id
    await iUser.postActivity({
      level: 1,
      type: 2,
      location: {
        module: 'apps',
        page: 'content',
        element: 'collect-icon',
        event: 'click'
      },
      action: '@content/collection',
      value: { id: id }
    })
    commit('pushContentCollection', content)
  },
  async removeContentCollection ({ commit }, content) {
    const id = content._id
    await iUser.postActivity({
      level: 1,
      type: 2,
      location: {
        module: 'apps',
        page: 'content',
        element: 'collect-icon',
        event: 'click'
      },
      action: '@content/collectionRemove',
      value: { id: id }
    })
    commit('deleteContentCollection', content)
  }
}
export default actions
